package com.hjc.controller;

import com.hjc.dto.RoleAuthDto;
import com.hjc.entity.CurrentUser;
import com.hjc.entity.Result;
import com.hjc.entity.Role;
import com.hjc.page.Page;
import com.hjc.service.RoleService;
import com.hjc.utils.TokenUtils;
import com.hjc.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private TokenUtils tokenUtils;

    @RequestMapping("/role-list")
    public Result roleList() {
        List<Role> roleList = roleService.queryAllRole();
        return Result.ok(roleList);
    }

//role/role-page-list?roleName=&roleCode=&roleState=&pageSize=5&pageNum=1&totalNum=0
    @RequestMapping("/role-page-list")
    public Result RoleList(Page page,Role role) {
        page = roleService.queryRoleByPage(page, role);
        return Result.ok(page);
    }

//     http://localhost:3000/api/role/role-add
//     {roleName: "d", roleDesc: "f", roleCode: "f"}
    @RequestMapping("role-add")
    public Result addRole(@RequestBody Role role,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int createBy = currentUser.getUserId();
        role.setCreateBy(createBy);
        return roleService.addRole(role);
    }

//    role/role-state-update
    @RequestMapping("/role-state-update")
    public Result setRoleState(@RequestBody Role role) {
        return roleService.setRoleState(role.getRoleId(), role.getRoleState());
    }
//     @RequestMapping("/role-state-update")
//     public Result setRoleState(@RequestParam("roleId") int roleId,@RequestParam("roleState")String roleState) {
//         return roleService.setRoleState(roleId, roleState);
//     }

//    role/role-delete/19
    @RequestMapping("/role-delete/{roleId}")
    public Result removeRoleById(@PathVariable Integer roleId) {
        return roleService.removeRoleByRoleId(roleId);
    }


//    role/role-update
    @RequestMapping("/role-update")
    public Result setRoleDescByRoleId(@RequestBody Role role,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        role.setCreateBy(currentUser.getUserId());
        return roleService.setRoleDescByRoleId(role);
    }

//    查询角色已经分配的权限http://localhost:3000/api/role/role-auth?roleId=22&_t=1696986190090
    @RequestMapping("/role-auth")
    public Result loadAuthTree(Integer roleId) {
        List<Integer> authIdList = roleService.findAuthIdListByRoleId(roleId);
        return Result.ok(authIdList);
    }

//    http://localhost:3000/api/role/auth-grant

    @RequestMapping("/auth-grant")
    public Result addRoleAuth(@RequestBody RoleAuthDto roleAuthDto) {
        roleService.addRoleAuth(roleAuthDto);
        return Result.ok("更新成功！");
    }

}
